package com.hk.mini;


public class MBUtil {

    /**
     * 获得CRC16校验码
     * @param bytes
     * @return
     */
    public static String getCRC(byte[] bytes){
        int CRC = 0x0000FFFF;
        int POLYNOMIAL = 0x0000A001;

        int i,j;

        for(i = 0;i<bytes.length;i++){

            CRC^= ((int)bytes[i]&0x000000FF);
            for (j = 0;j<8;j++){
                if ((CRC&0x00000001)!=0){
                    CRC>>=1;
                    CRC ^=POLYNOMIAL;
                }else {
                    CRC>>=1;
                }
            }
        }

        String result = String.format("%04x",CRC);

        //高低位互换
        return result.substring(2,4)+result.substring(0,2);
    }

    /**
     * 获得CRC16校验码
     * @param code
     * @return
     */
    public static String getCRC(String code){

        return getCRC(stringToHex(code));
    }

    /**
     * 获得CRC16校验码
     * @param bytes
     * @return
     */
    public static String getReaderCRC(byte[] bytes){
        int CRC = 0x0000FFFF;
        int POLYNOMIAL = 0x00008408;

        int i,j;

        for(i = 0;i<bytes.length;i++){

            CRC^= ((int)bytes[i]&0x000000FF);
            for (j = 0;j<8;j++){
                if ((CRC&0x00000001)!=0){
                    CRC>>=1;
                    CRC ^=POLYNOMIAL;
                }else {
                    CRC>>=1;
                }
            }
        }

        String result = String.format("%04x",CRC);

        //高低位互换
        return result.substring(2,4)+result.substring(0,2);
    }

    /**
     * 获得CRC16校验码
     * @param code
     * @return
     */
    public static String getReaderCRC(String code){

        return getReaderCRC(stringToHex(code));
    }

    /**
     * 16进制byte转换成字符串
     * @param bytes
     * @return
     */
    public static String hexToString(byte[] bytes){

        StringBuffer sb = new StringBuffer();

        //16进制转成字符串
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0XFF);
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);

        }

        return sb.toString();

    }

    /**
     * 字符串转换成16进制byte
     * @param code
     * @return
     */
    public static byte[] stringToHex(String code){

        byte[] baKeyword = new byte[code.length()/2];

        for (int i =0;i<baKeyword.length;i++){
            baKeyword[i] = (byte)(0xFF & Integer.parseInt(code.substring(i*2,i*2+2),16));
        }

        return baKeyword;
    }

    /**
     * 16进制转10进制
     * @param code
     * @return
     */
    public static int hexStringToInt(String code){
        return Integer.parseInt(code,16);
    }


    public static String byteToHexString(byte[] bytes){

        StringBuffer sb = new StringBuffer();

        for (byte b : bytes) {
            sb.append(String.format("%02x",b));
        }

        return sb.toString();
    }





    public static void main(String[] args) {

        //System.out.println(getCRC("010300000025"));


    }


}
